﻿namespace ManagerClinic.DanhMuc
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using DevExpress.XtraEditors;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Ultilities.Ultility;
    using ManagerClinic.Modules;
    using System.Linq;

    public partial class frmDmTinhThanh : DevExpress.XtraEditors.XtraForm
    {
        GridCheckMarksSelection gridCheckMarksSelection = null;
        DmTinhThanhBusiness dmTinhThanhBusiness = new DmTinhThanhBusiness();
        DmQuocGiaBusiness dmQuocGiaBusiness = new DmQuocGiaBusiness();

        IList<DmTinhThanhEntity> lstDmTinhThanhEntity = null;

        public frmDmTinhThanh()
        {
            InitializeComponent();

            gridCheckMarksSelection = new GridCheckMarksSelection(gvTinhThanh);
            LoadQuocGia();
        }

        private void LoadQuocGia()
        {
            var dmQuocGia = dmQuocGiaBusiness.GetDmQuocGiaList();
            var initalData = new DmQuocGiaEntity()
            {
                MaQuocGia = ConstantManagerClinic.sValueMember,
                TenQuocGia = ConstantManagerClinic.sDisplayMember
            };
            dmQuocGia.Insert(0, initalData);

            lkQuocGia.Properties.DataSource = dmQuocGia;
            lkQuocGia.Properties.DisplayMember = "TenQuocGia";
            lkQuocGia.Properties.ValueMember = "MaQuocGia";
            lkQuocGia.EditValue = ConstantManagerClinic.sValueMember;
        }

        private void LoadDataGrid(object sender, EventArgs e)
        {
            lstDmTinhThanhEntity = dmTinhThanhBusiness.GetDmTinhThanhList();

            if (lstDmTinhThanhEntity.Count > 0 && lkQuocGia.EditValue.ToString() != ConstantManagerClinic.sValueMember)
            {
                lstDmTinhThanhEntity = lstDmTinhThanhEntity.Where(n => n.MaQuocGia == lkQuocGia.EditValue.ToString()).ToList();
            }

            grdTinhThanh.DataSource = lstDmTinhThanhEntity;
        }

        private DmTinhThanhEntity GetDmTinhThanhEntity()
        {
            if (gvTinhThanh.RowCount > 0)
            {
                return (DmTinhThanhEntity)gvTinhThanh.GetFocusedRow();
            }

            return null;
        }

        private void frmDmTinhThanh_Load(object sender, EventArgs e)
        {
            Authenticate.IsAuthenticateForm(this);
            LoadDataGrid(sender, e);
        }

        private void gvTinhThanh_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
                e.Handled = false;
            }
        }

        private void btnThemMoi_Click(object sender, EventArgs e)
        {
            var frm = new frmCapNhatDmTinhThanh((int)ConstantManagerClinic.StateInsertOrUpdate.Insert);
            frm.ButtonClicked += new EventHandler(LoadDataGrid);
            frm.ShowDialog();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            DmTinhThanhEntity dmTinhThanhEntity = GetDmTinhThanhEntity();
            if (dmTinhThanhEntity == null) return;

            var frm = new frmCapNhatDmTinhThanh((int)ConstantManagerClinic.StateInsertOrUpdate.Update, dmTinhThanhEntity);
            frm.ButtonClicked += new EventHandler(LoadDataGrid);
            frm.ShowDialog();
        }

        private void grdTinhThanh_DoubleClick(object sender, EventArgs e)
        {
            if (btnSua.Enabled == false) return;

            btnSua_Click(sender, e);
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            var deleteObjects = gridCheckMarksSelection.selection;

            if (deleteObjects.Count <= 0)
            {
                MessageBox.Show("Bạn chưa chọn bản ghi để xóa!", ConstantManagerClinic.sTitleInformation, MessageBoxButtons.OK);
                return;
            }

            if (MessageBox.Show("Bạn chắc chắn muốn xóa dữ liệu?", ConstantManagerClinic.sTitleWarming, MessageBoxButtons.YesNo) == DialogResult.No)
            {
                gridCheckMarksSelection.ClearSelection();
                return;
            }

            foreach (DmTinhThanhEntity dm in deleteObjects)
            {
                dmTinhThanhBusiness.DeleteDmTinhThanh(dm.MaTinhThanh);
            }

            gridCheckMarksSelection.ClearSelection();
            LoadDataGrid(sender, e);
        }

        private void lkQuocGia_EditValueChanged(object sender, EventArgs e)
        {
            LoadDataGrid(sender, e);
        }

    }
}